package com.learn.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.eduapi.entity.order.PayOrder;
import com.learn.eduapi.entity.order.PayOrderRecord;
import com.learn.eduapi.entity.order.UserCourseOrder;
import com.learn.eduorder.mapper.OrderDao;
import com.learn.eduorder.mapper.PayOrderDao;
import com.learn.eduorder.mapper.PayOrderRecordDao;
import com.learn.eduorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;


    @Autowired
    private PayOrderDao payOrderDao;
    @Autowired
    private PayOrderRecordDao payOrderRecordDao;

    @Override
    public List<PayOrder> selectByUserId(Integer userId) {
        return payOrderDao.selectByUserId(userId);
    }

    @Override
    public int insertOrder(UserCourseOrder userCourseOrder) {
        //获取当前系统时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        userCourseOrder.setStatus("0");
        userCourseOrder.setCreateTime(dateString);
        userCourseOrder.setUpdateTime(dateString);
        userCourseOrder.setIsDel("0");
        return orderDao.insert(userCourseOrder);
    }

    @Override
    public int updateUserCourseOrder(UserCourseOrder userCourseOrderNew, Wrapper<UserCourseOrder> userCourseOrderOld) {
        return orderDao.update(userCourseOrderNew,userCourseOrderOld);
    }

    @Override
    public int updatePayOrder(PayOrder payOrder) {
        return payOrderDao.updatePayOrder(payOrder);
    }


    @Override
    public int insertPayOrderRecord(PayOrderRecord payOrderRecord) {
        //获取当前系统时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        payOrderRecord.setCreatedAt(dateString);
        return payOrderRecordDao.insert(payOrderRecord);
    }

    @Override
    public int updatePayOrderRecord(PayOrderRecord payOrderRecord) {
        return payOrderRecordDao.updatePayOrderRecord(payOrderRecord);
    }

    @Override
    public int deleteOrder(String orderNo) {
        QueryWrapper<UserCourseOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        return orderDao.delete(wrapper);
    }

    @Override
    public PayOrderRecord selectByOrderId(Integer orderNo) {
        return payOrderRecordDao.selectByOrderId(orderNo);
    }

    @Override
    public int insertPayOrder(PayOrder payOrder) {
        //获取当前系统时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = sdf.format(date);
        payOrder.setCreatedTime(dateString);
        payOrder.setUpdatedTime(dateString);
        return payOrderDao.insert(payOrder);
    }
}
